﻿define(['tools'], function () {

    var randomColorSegment = function () {
        return Math.round(255 * Math.random());
    };

    var createBokeh = function (target, top, left, size, r, g, b, callback) {
        var bokeh = $("<div />")
			.addClass("bokeh")
			.css({
			    'position': 'absolute',
			    'left': Math.floor(left) + 'px',
			    'top': Math.floor(top) + 'px',
			    '-moz-border-radius': Math.floor(size) / 2 + 'px',
			    '-webkit-border-radius': Math.floor(size) / 2 + 'px',
			    'border-radius': Math.floor(size) / 2 + 'px',
			    'z-index': -1000,
			    'border': '1px solid rgba(' + size + ', 0.7)',
			    // Gradients for Firefox
			    'background': '-moz-radial-gradient( contain, rgba(' + r + ',' + g + ',' + b + ', 0.1), rgba(' + r + ',' + g + ',' + b + ',0.6))',
			    'background': '-radial-gradient( contain, rgba(' + r + ',' + g + ',' + b + ', 0.1), rgba(' + r + ',' + g + ',' + b + ',0.6))',
			    'background': '-ms-radial-gradient( contain, rgba(' + r + ',' + g + ',' + b + ', 0.1), rgba(' + r + ',' + g + ',' + b + ',0.6))',

			    // Freaking ugly workaround to make gradients work for Safari too, by applying it to the background-image
			    'background-image': '-webkit-gradient(radial, center center, 0, center center, 70.5, from(rgba(' + r + ',' + g + ',' + b + ', 0.1)), to(rgba(' + r + ',' + g + ',' + b + ',0.6)))'
			});



        bokeh.animate({
            'left': Math.floor(left - (size / 2)) + 'px',
            'top': Math.floor(top - (size / 2)) + 'px',
            width: size + 'px',
            height: size + 'px'
        }, "slow");

        bokeh.appendTo(target);
        bokeh.fadeOut(30000, function () {
            bokeh.remove();
        });

        if (callback != undefined) {
            callback(target, top, left, size, r, g, b);
        }
    };

    var bokehBoard = function (target, onBokehCreated) {
        $(target).click(function (e) {
            if (e != undefined) {
                createBokeh(
                    target,
                    Math.floor(e.pageY),
                    Math.floor(e.pageX),
                    Math.floor(200 * Math.random()) + 50,
                    randomColorSegment(),
                    randomColorSegment(),
                    randomColorSegment(),
                onBokehCreated);
            }
        });
    };

    return {
        bokehBoard: bokehBoard,
        createBokeh: createBokeh,
        randomColorSegment: randomColorSegment
    };

});